package com.aiatomic.couting.widgets;

import java.text.SimpleDateFormat;
import java.util.List;

import com.aiatomic.couting.R;
import com.aiatomic.couting.models.GoodEntity;
import com.aiatomic.couting.util.ImageUtils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GoodAdapter extends ArrayAdapter<GoodEntity> {
	SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public GoodAdapter(Context context, int resource, List<GoodEntity> objects) {
		super(context, resource, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		GoodEntity dataItem = this.getItem(position);
		view = LayoutInflater.from(getContext()).inflate(R.layout.custom_good_list_item, null);
		ImageView mImageView = ((ImageView) view.findViewById(R.id.good_pic_url));
		switch (dataItem.getPic()) {
		case 1:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_year));
			break;
		case 2:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_half_year));
			break;
		case 3:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_month));
			break;
		case 4:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_half_month));
			break;
		case 5:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_week));
			break;
		default:
			mImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_damion));
		}
		if (dataItem.getType() == 1) {
			ImageView mTopRightImageView = ((ImageView) view.findViewById(R.id.good_pic_url_top_right));
			mTopRightImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_tuijian));
		} else if (dataItem.getType() == 2) {
			ImageView mTopRightImageView = ((ImageView) view.findViewById(R.id.good_pic_url_top_right));
			mTopRightImageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_new));
		}

		((TextView) view.findViewById(R.id.good_name)).setText(dataItem.getName());
		((TextView) view.findViewById(R.id.good_desc)).setText(dataItem.getDesc());
		((TextView) view.findViewById(R.id.good_give_day)).setText(String.valueOf(dataItem.getGive_days()));
		((TextView) view.findViewById(R.id.good_money)).setText(String.format("%.2f", dataItem.getPrice()));
		if(dataItem.getDiscount()>=1){
			((TextView) view.findViewById(R.id.good_discout_label)).setVisibility(View.GONE);
			((TextView) view.findViewById(R.id.good_real_price_label)).setVisibility(View.GONE);
			((TextView) view.findViewById(R.id.good_discout)).setVisibility(View.GONE);
			((TextView) view.findViewById(R.id.good_real_price)).setVisibility(View.GONE);
		}else{
			((TextView) view.findViewById(R.id.good_discout)).setText(String.format("%.1f折", dataItem.getDiscount()*10));
			((TextView) view.findViewById(R.id.good_real_price)).setText(String.format("%.2f", dataItem.getDiscount()*dataItem.getPrice()));
		}

		return view;
	}
}

